<template>
  <div>
    <div class="search">
      <el-row class="clear">
        <label> 标题:</label><el-input 
          v-model="q.title" 
          class="search-input" 
          clearable 
          placeholder="关键字"/>
        <label> 分类:</label><el-select 
          v-model="q.categoryId" 
          placeholder="分类">
          <el-option 
            v-for="item in categories"
            :key="item.id"
            :label="item.name"
            :value="item.categoryId"/>
        </el-select>
        <label> 状态:</label><el-select 
          v-model="q.status" 
          placeholder="状态">
          <el-option 
            v-for="item in status"
            :key="item.id"
            :label="item.name"
            :value="item.status"/>
        </el-select>
        <el-button 
          class="search-button" 
          type="primary" 
          @click="query()">查询</el-button>
        <el-button 
          class="add-button" 
          type="success" 
          @click="write()">写文章</el-button>
      </el-row>
    </div>
    <el-table
      v-loading="loading"
      :data="articleList"
      element-loading-text="拼命加载中"
      border
      style="width: 100%;"
      @selection-change="batchSelect">
      <el-table-column
        type="selection"
        width="55"/>
      <el-table-column
        prop="title"
        label="标题">
        <template slot-scope="props">
          <router-link :to="'/article/detail/'+ props.row.id">{{ props.row.title }}</router-link>
        </template>
      </el-table-column>
      <el-table-column
        prop="hits"
        label="点赞"
        width="100"/>
      <el-table-column
        prop="status"
        label="状态"
        width="100">
        <template slot-scope="props">
          <span v-text="props.row.status == 1 ? '已发布' : '草稿'"/>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="180">
        <template slot-scope="props">
          <router-link 
            :to="{params: {id: props.row.id}}" 
            tag="span">
            <el-button 
              type="info" 
              size="small" 
              icon="edit" 
              @click="handleEdit(props.$index, props.row)">修改</el-button>
          </router-link>
          <el-button 
            type="danger" 
            size="small" 
            icon="delete" 
            @click="handleDelete(props.$index, props.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 16px">
      <div style="float:left">
        <el-button
          slot="handler"
          :disabled="batchSelectArray.length === 0"
          type="danger"
          icon="delete"
          size="small"
          @click="batchDel">
          <span>批量删除</span>
        </el-button>
      </div>
      <div style="float:right">
        <el-pagination
          :current-page="q.pageIndex"
          :page-sizes="[10, 15, 20, 50]"
          :page-size="q.pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </div>
  </div>
</template>
<style>

</style>
<script type="babel">
import { SET_ARTICLE_LIST, DELETE_ARTICLE } from '../store/app/mutation-type';
export default {
  data() {
    return {
      q: {
        title: undefined,
        categoryId: undefined,
        statusId: undefined,
        pageIndex: 1,
        pageSize: 10
      },
      //请求时的loading效果
      loading: false,
      //批量选择数组
      batchSelectArray: []
    };
  },
  computed: {
    status() {
      return [
        { status: undefined, name: '--请选择--' },
        { status: 1, name: '已发布' },
        { status: 2, name: '草稿' }
      ];
    },
    categories() {
      return [
        { categoryId: 0, name: '--请选择--' },
        { categoryId: 1, name: 'Nodejs' },
        { categoryId: 2, name: 'Webpack' },
        { categoryId: 3, name: 'Egg' }
      ];
    },
    total() {
      return this.$store.state.articleTotal;
    },
    articleList() {
      return this.$store.state.articleList;
    }
  },
  methods: {
    fetchApi(store, route, json = {}) {
      console.log('>>>route', route);
      return store.dispatch(SET_ARTICLE_LIST, json);
    },
    query() {
      this.fetchApi(this.$store, this.$route, this.q);
    },
    write() {
      this.$router.push('/article/add');
    },
    handleSelectionChange(val) {
      console.log('handleSelectionChange', val);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.q.pageSize = val;
      this.fetchApi(this.$store, this.$route, this.q);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.q.pageIndex = val;
      this.fetchApi(this.$store, this.$route, this.q);
    },
    handleEdit(index, row) {
      this.$message(`你点击了编辑操作 index:${index}, id:${row.id}`);
    },
    handleDelete(index, row) {
      this.$store.dispatch(DELETE_ARTICLE, { id: row.id });
      this.$message(`删除[${row.title}]成功!`);
    },
    //批量选择
    batchSelect(val) {
      this.batchSelectArray = val;
    },
    //批量删除
    batchDel() {
      this.$confirm('将批量删除选择文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true;
        this.$message.success('删除成功');
        this.loading = false;
      });
    }
  },
};
</script>